package com.JupiterMoons;

import java.text.SimpleDateFormat;
import java.util.Calendar;
//import java.util.Date;
import java.util.TimeZone;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
//import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;

//Satellite 	Semi-Major Axis 	Synodic Period 	Starting Angle
//Io			5.9 Jupiter radii	1.7698605 days	49 degrees
//Europa		9.4 Jupiter radii	3.5540942 days	277 degrees
//Ganymede		15.0 Jupiter radii	7.1663872 days	300 degrees
//Callisto 		26.4 Jupiter radii	16.753552? days	78 degrees
//
//(Starting Date is Midnight UT, Dec 31, 1983 = JD 2445700.0) 

//For each satellite, divide D by the synodic period, to get the number of times that 
//the satellite has gone around Jupiter since the starting date, then multiply the 
//FRACTIONAL part of the result by 360 degrees, and add the starting angle. This gives 
//the current angular position, relative to Jupiter. (You only use the fractional part 
//of the result because any whole number of revolutions simply returns it to the starting 
//angle.) 

public class JupiterMoons extends Activity {
	
	DemoView demoview;
	MyCounter myTimer;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
Log.v("Debug", "Starting Jupiters moons");
		demoview = new DemoView(this);
		setContentView(demoview);
		myTimer = new MyCounter(15000, 1000);
		myTimer.SetView(demoview);
		
		myTimer.start();
    }
    
    public class DemoView extends View{
    	
    	double lastXPositions[] = {0.,0.,0.,0.};
        private long currentDateMilliseconds;
        //private int tzoneOffset = 0;
        Jupiter jupiter;


		public DemoView(Context context){
			super(context);
			
			boolean flag = false;
	        int ia=0;
	        if (ia == 90){
	            flag = true;
	        }

	        //get millisecs since 1/1/1970
	        StarDate tst = new StarDate(currentDateMilliseconds);
	        if (jupiter == null)
	            jupiter = new Jupiter(tst, flag);
	        
	        Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

            tst.setTime(now.getTimeInMillis());
            jupiter.setDate(tst);
	        
            XYCoord myMoon;
            double moonAngle;

	        for(int i=0; i<4; i++){
	        	myMoon = jupiter.getMoonXY(i);
	        	moonAngle = jupiter.getMoonAngle(i) * 180 / Math.PI;
	        	String str = String.format("%d -> %f, %f, Angle %f", i, myMoon.x, myMoon.y, moonAngle);
	        	Log.v("Debug", str);
	        }			
		}
		
		@Override protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
	        StarDate tst = new StarDate(currentDateMilliseconds);
	        if (jupiter == null)
	            jupiter = new Jupiter(tst, false);
	        
	        Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

            tst.setTime(now.getTimeInMillis());
            jupiter.setDate(tst);
	        
			int dWidth = getWidth();
			int dHeight = getHeight();
			
			String str = String.format("Width %d, Height %d", dWidth, dHeight);
			Log.v("Debug", str);
			
			Bitmap jupFull = BitmapFactory.decodeResource(getResources(), R.drawable.jupiter);
			
			int nScaleX = (dWidth/2) / 30 * 2;  //allow for 30 radii, furthest ~26, and x2 for diameter vs radii
			Bitmap jupScaled = Bitmap.createScaledBitmap(jupFull, nScaleX, nScaleX, false);
			canvas.drawBitmap(jupScaled, dWidth/2-nScaleX/2, dHeight/2-nScaleX/2, null);
			
			Paint paint = new Paint();
			for(int i=0; i<4; i++){
				paint.setColor(Color.RED);
				XYCoord moonCoord = jupiter.getMoonXY(i);
				float x = (float) (dWidth/2  + moonCoord.x * nScaleX/2);
				float y = (float) (dHeight/2 + moonCoord.y * nScaleX/2);
				canvas.drawCircle(x, y, 2, paint);
				String moonLabel = jupiter.getMoonLabel(i);
				if( lastXPositions[i] < moonCoord.x ){
					moonLabel += "->";
				}
				else{
					moonLabel = "<-" + moonLabel;
				}
				canvas.drawText(moonLabel, x, y+25, paint);
				lastXPositions[i] = moonCoord.x;
			}
			
           SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");

			canvas.drawText(formatter.format(now.getTime()), 10, 50, paint);
		}
    }
}